package com.v1.web.filter;

import java.lang.reflect.Method;

import org.directwebremoting.AjaxFilter;
import org.directwebremoting.AjaxFilterChain;

import com.v1.web.action.BaseDwrAction;

/**
 * @author luyang
 * @create_time 2009/09/01
 */
public class DwrFilter implements AjaxFilter {

	public Object doFilter(Object obj, Method method, Object[] params, AjaxFilterChain chain)
			throws Exception {
		// WebContext webContext = WebContextFactory.get();
		// HttpSession session = webContext.getSession();
		// if (session.getAttribute(Constants.USER) == null || session.isNew())
		// {
		// HttpServletResponse response = webContext.getHttpServletResponse();
		// response.sendError(HttpServletResponse.SC_FORBIDDEN);
		// return "SC_FORBIDDEN";
		// }
		if (!(obj instanceof BaseDwrAction)) {
			return chain.doFilter(obj, method, params);
		}

		Object before = ((BaseDwrAction) obj).before();
		if (before != null) {
			return before;
		}
		Object doFilter = chain.doFilter(obj, method, params);
		Object after = ((BaseDwrAction) obj).after();
		if (after != null) {
			return after;
		}
		return doFilter;
	}

}
